//
//  NSString+Utility.m
//  LXVolunteer
//
//  Created by 李涛 on 15/5/20.
//  Copyright (c) 2015年 lexue. All rights reserved.
//

#import "NSString+Utility.h"
#import <CoreText/CoreText.h>
#import <CommonCrypto/CommonDigest.h>


@implementation NSString (Utility)
@dynamic successNoti;
@dynamic failNoti;
@dynamic realLength;

- (CGSize)getSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size{
        NSDictionary * attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
        return [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
}

- (CGSize)getSizeWithFont:(UIFont *)font{
        NSDictionary * attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
        return [self sizeWithAttributes:attributes];

}

- (CGSize)getSizeWithAtt:(NSDictionary *)att ConstrainedToSize:(CGSize)size{
    return [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:att context:nil].size;
}
- (CGSize)getSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size AndLineHeight:(CGFloat)lineHeight{
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineHeight];
    NSDictionary * attributes = @{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle};
    return [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
}
- (CGSize)getSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size AndParagraphStyle:(NSParagraphStyle *)paragraphStyle{
    NSDictionary * attributes = @{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle};
    return [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
}

+ (NSString *)safeString:(NSString *)str{
    if (!str) {
        return @"";
    }
    else {
        return str;
    }
}
- (NSUInteger)realLength{
    return  [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length;
}
- (NSString *)getRealStr{
    return  [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (NSString *)successNoti{
    return [NSString stringWithFormat:@"%@SuccessNoti",self];
}

- (NSString *)failNoti{
    return [NSString stringWithFormat:@"%@FailNoti",self];
}



- (BOOL)checkPhoneNum{
    if (self.length != 11) {
        return NO;
    }
    NSString *phoneRegex = @"^(1[0-9])\\d{9}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:self];
}

- (NSAttributedString *)getAttributedStrWithUnderLine{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self];
    NSRange strRange = {0,[str length]};
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
    return str;
}

- (NSAttributedString *)getAttributedStrWithUnderLineAndColor:(UIColor *)color{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self];
    NSRange strRange = {0,[str length]};
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
    [str addAttribute:NSForegroundColorAttributeName value:color range:strRange];
    return str;
}

- (NSString *)getMD5{
    const char *cStr = [self UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), result );
    return [NSString  stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3], result[4],
            result[5], result[6], result[7],
            result[8], result[9], result[10], result[11], result[12],
            result[13], result[14], result[15]
            ];
}

+ (NSString *)getCurrentTimeWithType:(NSString *)timeType{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:timeType];
    NSString * dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}
/**
 *  数量转换
 */
-(NSString *)getShotCount{
    NSInteger times = self.integerValue;
    if (times <= 999) {
        return [NSString stringWithFormat:@"%zd", times];
    }
    if (times < 99900) {
        NSInteger thousand = 1000;
        NSInteger left = times % thousand;
        times = (times - left) / thousand;
//        if (left >= 0) {
//            times += 1;
//        }
        left =left/100;
        return [NSString stringWithFormat:@"%zd.%zdk", times,left];
    }
//    NSInteger oneThousand = 1000;
//    if (times < oneThousand) {
//        return [NSString stringWithFormat:@"%zd", times];
//    }
//    if (times >= oneThousand) {
//        if (times>=99900) {
//            return @"99.9k";
//        }
//        return [NSString stringWithFormat:@"%.1fk", times/1000.0f];
//    }
//    
    return @"99.9k";
}
- (NSString*)getTimeFormat{
    if (self.realLength == 0) {
        return nil;
    }
    
    NSString *reFormat =@"";
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY年MM月dd日"];
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:self.integerValue];
    
    NSInteger now =[[NSDate date] timeIntervalSince1970];
    
    //来自未来
    if (self.integerValue>now) {
        reFormat = [formatter stringFromDate:confromTimesp];
    }
    if (self.integerValue==now) {
        reFormat = @"现在";
    }
    NSInteger sub =now-self.integerValue;
    if (sub>1&&sub<60) {
        reFormat = [NSString stringWithFormat:@"%ld秒前",(long)sub];
    }
    
    if (sub>60&&sub<60*60) {
        reFormat = [NSString stringWithFormat:@"%ld分钟前",(long)sub/60];
    }
    
    if (sub>60*60&&sub<60*60*24) {
        reFormat = [NSString stringWithFormat:@"%ld小时前",(long)sub/60/60];
    }
    
    if (sub>60*60*24&&sub<60*60*24*7) {
        reFormat = [NSString stringWithFormat:@"%ld天前",(long)sub/60/60/24];
    }
    
    if (sub>60*60*24*7) {
        NSDateFormatter *f = [[NSDateFormatter alloc] init];
        [f setDateStyle:NSDateFormatterMediumStyle];
        [f setTimeStyle:NSDateFormatterShortStyle];
        [f setDateFormat:@"YYYY"];
        
        NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
        [f2 setDateStyle:NSDateFormatterMediumStyle];
        [f2 setTimeStyle:NSDateFormatterShortStyle];
        [f2 setDateFormat:@"MM月dd日"];
        
        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:self.integerValue];
        NSString *str1= [f stringFromDate:confromTimesp];
        NSString *str2 =[f stringFromDate:[NSDate dateWithTimeIntervalSince1970:now]];
        if ([str1 isEqualToString:str2]) {
            reFormat = [f2 stringFromDate:confromTimesp];
        }else{
            reFormat =[formatter stringFromDate:confromTimesp];
        }
    }
    return reFormat;
}

+  (NSString *)getTimeMonthDay:(NSString *)monthAndDay{
    
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy-MM-dd"]; // 年-月-日 时:分:秒
    
    NSDate * date = [formatter dateFromString:monthAndDay];
    
    NSDateFormatter * formatter1 = [[NSDateFormatter alloc] init];
    
    [formatter1 setDateFormat:@"MM月dd号"]; // 年-月-日 时:分:秒
    
    NSString *str = [formatter1 stringFromDate:date];
    
    NSString *zero = [str substringToIndex:1];
    
    if([zero isEqualToString:@"0"]) {
        
        str = [str substringFromIndex:1];
    }
    return str;
}
+ (NSString *)getWeekDayFordate:(NSString *)monthAndDay
{
    NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy-MM-dd"]; // 年-月-日 时:分:秒
    
    NSDate * date = [formatter dateFromString:monthAndDay];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:date];
    
    NSString *weekStr = [weekday objectAtIndex:components.weekday];
    return weekStr;
}
+ (NSString *)getTime:(NSString *)shijianchuo {

    NSString *str=shijianchuo ;//时间戳
    NSTimeInterval time=[str doubleValue]+28800;//因为时差问题要加8小时 == 28800 sec
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    NSLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];


    return currentDateStr;
}


@end
